package com.breeze.toolkit.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.breeze.toolkit.util.ConstantsUtil;
import com.breeze.toolkit.util.ResUtil;

/**
 * Created by john on 2016/9/1.
 */
public abstract class BaseFragment extends Fragment implements
		RefreshView.OnRefreshListener, UIUpdateListener {
	protected Context mContext;
	private RefreshView mRefreshView;
	private boolean mIsAdjust = false;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
	                         @Nullable ViewGroup container,
	                         @Nullable Bundle savedInstanceState) {
		if (getContentView() != 0) {
			FrameLayout frameLayout = new FrameLayout(getActivity());
			frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

			View bottom = inflater.inflate(getContentView(), container, false);
			frameLayout.addView(bottom);

			mRefreshView = new RefreshView(mContext);
			mRefreshView.setRefreshListener(this);
			frameLayout.addView(mRefreshView);

			return frameLayout;
		}

		return super.onCreateView(inflater, container, savedInstanceState);
	}

	public void showRefreshView() {
		if (mRefreshView != null) {
			mRefreshView.setRefreshable(true);
			mRefreshView.updateVisibility(true);
		}
	}

	/**
	 * @param notice 无内容时展示文案
	 * @param solution 无内容时提示文案，可为null
	 */
	public void showContentEmptyView(String notice, String solution) {
		if (mRefreshView != null) {
			try {
				mRefreshView.setViewContent(ResUtil.getDrawableId(mContext, ConstantsUtil.TIP_EMPTY_IMAGE_NAME), notice, solution);
				mRefreshView.setRefreshable(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
			mRefreshView.updateVisibility(true);
		}
	}

	/**
	 * 调整刷新页面布局位置，某些页面特殊需求用
	 * @param topMargin 刷新页面顶部边距
	 */
	public void adjustRefreshViewTopMargin(int topMargin) {
		if (mRefreshView != null) {
			RelativeLayout.LayoutParams pa = (RelativeLayout.LayoutParams) mRefreshView.getLayoutParams();
			if (pa != null) {
				pa.topMargin = topMargin;
			}
		}
	}

	/**
	 * 调整刷新页面的布局位置
	 * @param header ListView的header部分, 若只部分展示无网刷新页面, 在ListView的addHeaderView后调用此方法即可
	 */
	public void adjustRefreshViewWithHeader(final View header) {
		if (header != null) {
			header.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
				@Override
				public void onGlobalLayout() {
					if (!mIsAdjust && header.getHeight() > 0) {
						mIsAdjust = true;
						adjustRefreshViewTopMargin(header.getHeight());
					}
				}
			});
		}
	}

	@Override
	public void refreshRequest() {

	}

	/**
	 * 网络连接异常时UI展示接口
	 *
	 * @param isConnected true 服务器连接失败, false 无网络
	 */
	@Override
	public void showConnectExceptionView(boolean isConnected) {
		if (isConnected) {
			mRefreshView.setViewContent(ResUtil.getDrawableId(mContext, ConstantsUtil.TIP_EMPTY_IMAGE_NAME),
			                            getString(ConstantsUtil.STRING_TIP_SERVER_ERROR_NAME),
			                            getString(ConstantsUtil.STRING_TIP_SOLUTION_NAME));
		}
		showRefreshView();
	}

	/**
	 * 服务端无数据返回时UI展示接口
	 */
	@Override
	public void showEmptyContentView() {
		showContentEmptyView(getString(ConstantsUtil.STRING_TIP_NO_DATA_NAME), null);
	}

	private String getString(String name) {
		return mContext.getResources().getString(ResUtil.getStringId(mContext, name));
	}

	public abstract int getContentView();
}
